package com.guguskins.back.controller;


import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuBox;
import com.guguskins.back.entity.GuBoxItem;
import com.guguskins.back.entity.GuGoods;
import com.guguskins.back.entity.bo.*;
import com.guguskins.back.entity.vo.*;
import com.guguskins.back.service.GuBoxService;
import com.guguskins.back.utils.CommonUtil;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 盲盒 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/box")
public class GuBoxController {

    @Autowired
    private GuBoxService boxService;

    /**
     * 后台分页
     */
    @GetMapping("/back/page")
    public ResultVo backPage(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                             @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                             @RequestParam(value = "category", required = false, defaultValue = "-1") int category,
                             @RequestParam(value = "state", required = false, defaultValue = "-1") int state,
                             @RequestParam(value = "type", required = false, defaultValue = "-1") int type,
                             @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        PageVo<GuBox> boxPageVo = boxService.backPage(p, size, category, state, type, searchKey);
        return ResultVo.success(boxPageVo);
    }

    /**
     * 后台添加
     */
    @PostMapping("/back/add")
    public ResultVo backAdd(@RequestBody @Validated SaveBoxBo saveBoxBo) {
        if (boxService.backAdd(saveBoxBo)) {
            return ResultVo.success("创建成功");
        }
        return ResultVo.fail("创建失败");
    }

    /**
     * 盒子详情
     */
    @GetMapping("/back/detail/{boxId}")
    public ResultVo backDetail(@PathVariable("boxId") long boxId) {
        GuBox box = boxService.backDetail(boxId);
        return ResultVo.success(box);
    }

    /**
     * 上下架
     */
    @PostMapping("/back/changeState")
    public ResultVo backChangeState(@RequestBody GuBox box) {
        if (boxService.backChangeState(box)) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    /**
     * 后台修改
     */
    @PostMapping("/back/edit")
    public ResultVo backEdit(@RequestBody @Validated EditBoxBo editBoxBo) {
        if (boxService.backEdit(editBoxBo)) {
            return ResultVo.success("提交成功");
        }
        return ResultVo.fail("提交失败");
    }

    /**
     * 修改概率
     */
    @PostMapping("/back/changeProbability")
    public ResultVo backChangeProbability(@RequestBody GuBoxItem boxItem) {
        if (boxService.backChangeProbability(boxItem)) {
            return ResultVo.success("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    /**
     * 获取箱子内没有的饰品
     */
    @GetMapping("/back/goodsListForIsNo")
    public ResultVo backGoodsListForIsNo(@RequestParam("boxId") long boxId,
                                         @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {
        List<GuGoods> goodsList = boxService.backGoodsListForIsNo(boxId, searchKey);
        return ResultVo.success(goodsList);
    }

    /**
     * 添加
     */
    @PostMapping("/back/addGoods")
    public ResultVo backAddGoods(@RequestBody SaveBoxGoodsBo saveBoxGoodsBo) {
        if (boxService.backAddGoods(saveBoxGoodsBo)) {
            return ResultVo.success("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    /**
     * 剔除
     */
    @PostMapping("/back/removeGoods")
    public ResultVo backRemoveGoods(@RequestBody GuBoxItem boxItem) {
        if (boxService.backRemoveGoods(boxItem)) {
            return ResultVo.success("删除成功");
        }
        return ResultVo.fail("删除失败");
    }

    /**
     * 盲盒详情页
     */
    @GetMapping("/{boxId}")
    public ResultVo getBoxCapacity(@PathVariable("boxId") Long boxId) {
        GuBox box = boxService.getBoxCapacity(boxId);
        return ResultVo.success(box);
    }

    /**
     * 盲盒开奖
     */
    @PostMapping("/luckDraw")
    @Authorized
    public ResultVo luckDraw(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                             @RequestBody @Validated LuckDrawBo luckDrawBo) {
        if (!ObjectUtils.isEmpty(token)) {
            Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
            luckDrawBo.setUserId(userId);
        }
        LuckDrawVo result = boxService.luckDraw(luckDrawBo);
        return ResultVo.success(result);
    }

    @PostMapping("/luckDrawInventory")
    @Authorized
    public ResultVo luckDrawInventory(@RequestHeader("token") String token,
                                      @RequestBody @Validated LuckDrawInventoryBo luckDrawInventoryBo) {
        if (!ObjectUtils.isEmpty(token)) {
            Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
            luckDrawInventoryBo.setUserId(userId);
        }
        LuckDrawVo result = boxService.luckDrawInventory(luckDrawInventoryBo);
        return ResultVo.success(result);
    }

    /**
     * 道具站步骤一
     */
    @GetMapping("/propStandData")
    public ResultVo propStandData() {
        List<GuBox> boxes = boxService.getBoxes(1);
        return ResultVo.success(boxes);
    }

    /**
     * 道具站步骤二
     */
    @GetMapping("/propStandData/{boxId}")
    public ResultVo propStandDataDetail(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                                        @PathVariable("boxId") Long boxId,
                                        @RequestParam(value = "type", required = false, defaultValue = "0") int type) {
        Long userId = (long) CommonUtil.generateInt();
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        PropStandDataDetailVo propStandDataDetailVo = boxService.propStandDataDetail(type, userId, boxId);

        return ResultVo.success(propStandDataDetailVo);
    }

    /**
     * 道具站步骤三
     */
    @GetMapping("/propStandData/{boxId}/{id}")
    public ResultVo propStandDataDetailById(@RequestHeader("token") String token,
                                            @PathVariable("boxId") Long boxId,
                                            @PathVariable("id") int id,
                                            @RequestParam("uuid") Long uuid) {
        Long userId = uuid;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }

        PropStandDataDetailVo propStandDataDetailVo = boxService.propStandDataDetailById(userId, boxId, id);

        return ResultVo.success(propStandDataDetailVo);
    }

    /**
     * 展示奖品池
     */
    @GetMapping("/goodsListShow/{boxId}")
    public ResultVo goodsListShow(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                                  @PathVariable("boxId") Long boxId,
                                  @RequestParam(value = "id", required = false, defaultValue = "") Integer id,
                                  @RequestParam(value = "uuid", required = false, defaultValue = "") Long uuid) {

        Long userId = uuid;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        List<GuGoods> goodsListShow = boxService.goodsListShow(userId, boxId, id);
        return ResultVo.success(goodsListShow);
    }

    /**
     * 使用道具卡（一般没有返回值）
     * 注：使用提示卡会返回奖品池
     */
    @PostMapping("/card")
    @Authorized
    public ResultVo recordCard(@RequestHeader("token") String token, @Validated @RequestBody UseCardBo useCardBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        Object result = boxService.recordCard(userId, useCardBo);
        return ResultVo.success(result, "使用成功");
    }


    /**
     * 使用道具卡（一般没有返回值）
     * 注：使用提示卡会返回奖品池
     */
    @PostMapping("/cancelCard")
    @Authorized
    public ResultVo cancelCard(@RequestHeader("token") String token, @Validated @RequestBody UseCardBo useCardBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        Object result = boxService.cancelCard(userId, useCardBo);
        return ResultVo.success(result, "取消成功");
    }

    /**
     * 道具站开奖
     */
    @PostMapping("/prize")
    @Authorized
    public ResultVo getPrize(@RequestHeader("token") String token, @Validated @RequestBody UseCardBo useCardBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        BoxPrizeVo boxPrizeVo = boxService.getPrize(userId, useCardBo);
        return ResultVo.success(boxPrizeVo);
    }

    /**
     * 道具站第四步开奖
     */
    @PostMapping("/whitePrize")
    @Authorized
    public ResultVo getWhitePrize(@RequestHeader("token") String token, @Validated @RequestBody UseCardBo useCardBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        List<BoxPrizeVo> boxPrizeVos = boxService.getWhitePrize(userId, useCardBo);
        return ResultVo.success(boxPrizeVos);
    }

    /**
     * 道具站第四步放入库存
     */
    @PostMapping("/putInventory")
    @Authorized
    public ResultVo putInventory(@RequestHeader("token") String token, @RequestBody UseCardBo useCardBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        return ResultVo.success(boxService.putInventory(userId, useCardBo));
    }

    /**
     * 道具站第四步放入库存
     */
    /*@PostMapping("/exchangeGuCurrency")
    @Authorized
    public ResultVo exchangeGuCurrency(@RequestHeader("token") String token, @RequestBody UseCardBo useCardBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        boxService.exchangeGuCurrency(userId, useCardBo);
        return ResultVo.success("兑换成功");
    }*/
    @PostMapping("/back/findBox")
    public ResultVo findBox(@RequestParam(value = "boxId", required = false, defaultValue = "1") int boxId) {
        return ResultVo.success(boxService.getById(boxId));
    }
/*
    @GetMapping("/back/list")
    public ResultVo getList(@RequestParam String searchKey ) {
        List<GuBox> list = boxService.list(new LambdaQueryWrapper<GuBox>() {
            {
                like(GuBox::getBoxName, searchKey);
            }
        });
        return ResultVo.success(list);
    }*/
}
